package com.bjpowernode.stusys.controller;

import com.bjpowernode.stusys.entity.Student;
import com.bjpowernode.stusys.exception.SysException;
import com.bjpowernode.stusys.service.StudentService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2021/12/1 11:42
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     * 通过传参修改pageNum完成下一页和上一页
     * @param pageNum
     * @param model
     * @return
     */
/*    @RequestMapping("findByPage")
    public String findStuByPage(Student student,
                             int pageNum,
                             Model model,
                             @RequestParam(defaultValue = "7") Integer pageSize){
        PageInfo<Student> page = studentService.findPage(student, pageNum, pageSize);
        model.addAttribute("page",page);
        return "main";
    }*/

    @RequestMapping("/page")
    public String findStuByPage(Model model,
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "7") Integer pageSize,
                                @RequestParam(required = false) String sname,
                                @RequestParam(required = false) Integer sage){
        Student student = null;
        if (sname != null || sage != null){
            student = new Student();
            student.setSname(sname);
            student.setSage(sage);
        }
        PageInfo<Student> page = studentService.findPage(student, pageNum, pageSize);
        if (page.getTotal() == 0){
            return "redirect:/student/page?pageNum=" + 1;
        }else if (page.getList().size() == 0){
            return "redirect:/student/page?pageNum=" + (pageNum-1);
        }
        model.addAttribute("page",page);
        model.addAttribute("sname",sname);
        model.addAttribute("sage",sage);
        return "main";
    }

    /**
     * 删除学生
     * @param id
     * @param pageNum
     * @return
     */
    @RequestMapping("remove")
    public String deleteStuById(int id,Integer pageNum){
        studentService.deleteById(id);
        return "redirect:/student/page?pageNum=" + pageNum;
    }

    /**
     * 添加学生页面
     * @return
     */
    @RequestMapping("/addUI")
    public String addUI(){
        return "add";
    }


    /**
     * 添加学生
     * @param student
     * @return
     */
    @RequestMapping("/saveStu")
    public String saveStu(Student student){
        studentService.addStu(student);
        return "redirect:/student/page";
    }

    /**
     * 编辑学生页面
     * @return
     */
    @RequestMapping("/editUI")
    public String editUI(Student student,Model model){
        Student student1= studentService.findById(student.getId());
        model.addAttribute("student",student1);
        return "edit";
    }

    /**
     * 修改学生信息
     * @return
     */
    @RequestMapping("/editStu")
    public String editStuById(Student student){
        studentService.edit(student);
        return "redirect:/student/page";
    }
}
